package han.bashe.web.servlet;

import han.bashe.business.ejb.LogQuery;
import han.bashe.business.ejb.Pipeline;
import han.bashe.common.converter.InternalConverter;
import han.bashe.common.entity.BasheEntityConstants;
import han.bashe.common.entity.SoaRequestMessage;
import han.bashe.common.exception.BasheErrorCode;
import han.bashe.common.exception.BasheException;
import han.bashe.common.message.NormalizedMessage;
import han.bashe.common.message.RequestHelper;
import han.bashe.common.util.ExceptionUtils;
import han.bashe.common.util.RetryUtils;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlObject;

import v1.log.bifang.han.RetryRequestDocument;
import v1.log.bifang.han.RetryResponseDocument;
import v1.message.bashe.han.BasheErrorDocument;
import v1.message.bashe.han.BasheMessageDocument;

/**
 * Servlet implementation class for Servlet: RetryServlet
 * 
 */
public class RetryServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	@EJB
	private Pipeline pipeline;

	@EJB
	private LogQuery query;
	
	private static final Log log = LogFactory.getLog(RetryServlet.class);

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public RetryServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String xml = ServletReqResHeler.getRequestString(request);
			log.info("request is " + xml);
			RetryRequestDocument doc = RetryRequestDocument.Factory.parse(xml);
			if (!doc.validate()) {
				throw new BasheException(BasheErrorCode.PARSE_CLIENT_XML_ERROR,
						"request xml is invalid");
			}
			RetryResponseDocument resDoc = RetryResponseDocument.Factory
					.newInstance();
			resDoc.addNewRetryResponse();
			resDoc.getRetryResponse().setStatus(BasheEntityConstants.STATUS_OK);
			SoaRequestMessage reqMsg = query.getRequestMessage(doc
					.getRetryRequest().getMessageLogId());
			BasheMessageDocument bsDoc = BasheMessageDocument.Factory
					.parse(reqMsg.getMessage());
			NormalizedMessage nm = InternalConverter.bashe2Normalized(bsDoc);
			if (reqMsg != null) {
				RequestHelper rh = new RequestHelper(nm);
				rh.setRetryLogId(doc
					.getRetryRequest().getMessageLogId());
				
				if (doc.getRetryRequest().getRequest() != null) {
					rh.setRetryData(true);
					
					XmlObject xo = RetryUtils.getRetryRequest(doc);
					nm.setBody(xo.toString());
				} 
				
				log.info("retry message is " + nm);
				try {
					pipeline.process(nm);
				} catch (Exception e) {
					resDoc.getRetryResponse().setStatus(
							BasheEntityConstants.STATUS_ERROR);
				}
				
			}
			
			ServletReqResHeler.setResponseString(response, resDoc.toString());
		} catch (BasheException e) {
			
			log.error("RetryServlet.doPost() error", e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(e.getCode());
			error.getBasheError().setErrorMsg(e.getMsg());
			error.getBasheError().setErrorTrace(
					ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());

		} catch (Exception e) {
			
			log.error("RetryServlet.doPost() error", e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(BasheErrorCode.UNKNOWN_ERROR);
			error.getBasheError().setErrorMsg(e.getMessage());
			error.getBasheError().setErrorTrace(
					ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());
		}
	}
}